/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.common.web.Util;
import com.koron.scene.domain.Enterprise;
import com.koron.scene.service.api.EnterpriseService;
import com.koron.scene.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "Enterprise", description = "相关方接口")
@RestController
@RequestMapping(value = "/enterprise")
public class EnterpriseController {
    private final static Logger logger = LoggerFactory.getLogger(EnterpriseController.class);

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private Util util;

    @RequestMapping(value = "/add.htm", method = RequestMethod.POST)
    @ApiOperation(value = "企业信息-保存企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "企业名称", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseType", value = "企业类型", dataType = "String"),
            @ApiImplicitParam(name = "registerCapital", value = "注册资金", dataType = "double"),
            @ApiImplicitParam(name = "address", value = "企业地址", dataType = "String"),
            @ApiImplicitParam(name = "businessScope", value = "业务范围", dataType = "String"),
            @ApiImplicitParam(name = "legalPerson", value = "法人", dataType = "String"),
            @ApiImplicitParam(name = "establishDateStr", value = "创建日期", dataType = "String"),
            @ApiImplicitParam(name = "phoneNumber", value = "联系电话", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
    })
    @Log(title = "新增相关方企业信息",businessModule = "相关方档案", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> add(String name, String enterpriseType, Double registerCapital, String address,
                              String businessScope, String legalPerson, String establishDateStr, String phoneNumber,
                              String corpCode, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return enterpriseService.add(name, enterpriseType, registerCapital, address, businessScope, legalPerson, establishDateStr, phoneNumber, corpCode, user);
    }

    @RequestMapping(value = "/update.htm", method = RequestMethod.POST)
    @ApiOperation(value = "企业信息-修改企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseId", value = "企业Id", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "企业名称", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseType", value = "企业类型", dataType = "String"),
            @ApiImplicitParam(name = "registerCapital", value = "注册资金", dataType = "double"),
            @ApiImplicitParam(name = "address", value = "企业地址", dataType = "String"),
            @ApiImplicitParam(name = "businessScope", value = "业务范围", dataType = "String"),
            @ApiImplicitParam(name = "legalPerson", value = "法人", dataType = "String"),
            @ApiImplicitParam(name = "establishDateStr", value = "创建日期", dataType = "String"),
            @ApiImplicitParam(name = "phoneNumber", value = "联系电话", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
    })
    @Log(title = "修改企业信息",businessModule = "相关方档案", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> update(String enterpriseId, String name, String enterpriseType, Double registerCapital, String address,
                                 String businessScope, String legalPerson, String establishDateStr, String phoneNumber,
                                 String corpCode, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return enterpriseService.update(enterpriseId, name, enterpriseType, registerCapital, address, businessScope, legalPerson,
                establishDateStr, phoneNumber, corpCode, user);
    }

    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    @ApiOperation(value = "企业信息-删除企业信息")
    @ApiImplicitParam(name = "enterpriseId", value = "企业Id", dataType = "String")
    @Log(title = "删除企业信息",businessModule = "相关方档案", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                 String enterpriseId) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return enterpriseService.delete(user, enterpriseId);
    }

    @RequestMapping(value = "/details.htm", method = RequestMethod.POST)
    @ApiOperation(value = "企业信息-企业信息详情")
    @ApiImplicitParam(name = "enterpriseId", value = "企业Id", dataType = "String")
    @Log(title = "删除企业信息",businessModule = "相关方档案", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                 String enterpriseId) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return enterpriseService.details(user, enterpriseId);
    }


    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    @ApiOperation(value = "企业信息-获取企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "企业名称", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseType", value = "企业类型", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
            @ApiImplicitParam(name = "keyword", value = "关键词", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "Integer", paramType = "query")
    })
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                               String name, String enterpriseType, String corpCode,String keyword, Integer page, Integer pageSize) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return enterpriseService.list(user, name, enterpriseType, corpCode,keyword, page, pageSize);
    }


    @RequestMapping(value = "/selectList.htm", method = RequestMethod.GET)
    @ApiOperation(value = "项目管理-项目相关方-获取企业下拉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "企业名称", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseType", value = "企业类型", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "Integer", paramType = "query")
    })
    public MessageBean<?> selectList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                     String name, String enterpriseType, String corpCode, Integer page, Integer pageSize) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return enterpriseService.selectList(user,name, corpCode);
    }


    @ApiOperation("下载模板")
    @RequestMapping(value = "/exportTemplate.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportExcelTemplate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null == user) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/enterprise.xlsx");
        try {
            InputStream initialStream = getClass().getClassLoader().getResourceAsStream("static/excel/enterprise.xlsx");
            FileUtils.copyInputStreamToFile(initialStream, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("相关方档案模板", targetFile, "default.btl", map);
    }

    @ApiOperation("导入相关方档案")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "上传的excel文件", required = true, paramType = "query", dataType = "file")
    })
    @RequestMapping(value = "/importExcel.htm", method = RequestMethod.POST)
    public MessageBean<?> importExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String userCode,
                                      @RequestParam(required = true) final MultipartFile file) {

        if (null == user) {
            //用于解决postman测试文件上传试的登陆问题
            List<StaffBean> staffBeanList = (List<StaffBean>) util.staff(userCode, null).getData();
            if (null != staffBeanList && staffBeanList.size() > 0) {
                user = staffBeanList.get(0);
            }
        }
        MessageBean<?> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        if (null == file) {
            msg.setCode(BusinessCode.FILE_IS_EMPTY.getCode());
            msg.setDescription(BusinessCode.FILE_IS_EMPTY.getDescription());
            return msg;
        }
        return enterpriseService.importExcel(user, file);
    }


    @ApiOperation("相关方档案列表导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "企业名称", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseType", value = "企业类型", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
    })
    @RequestMapping(value = "/exportList.htm", method = RequestMethod.GET)
    public HttpEntity<?> export(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                String name, String enterpriseType, String corpCode) {
        if (null == user) {
            return null;
        }
        List<Enterprise> list = enterpriseService.exportList(user, corpCode, name,enterpriseType);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File targetFile = new File("static/excel/enterpriseList.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/enterpriseList.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("相关方档案列表", targetFile, "enterpriseList.btl", map);
    }
}
